package com.kakavr.order.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.beust.jcommander.internal.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.order.service.IPurchaseHistoryService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.ConvertUtils;
import com.kakavr.util.ValidatorUtils;

@RestController
@RequestMapping("/n/purchase")
public class PurchaseHistoryController extends BaseController {
	@Resource
	private IPurchaseHistoryService purchaseHistoryService;

	/**
	 * 创建消费单
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request) {
		String params = request.getParameter("params");
		if(StringUtils.isBlank(params)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "参数");
		}
		Map<String,Object> paramMap = ConvertUtils.jsonToMap(params);
		// 店铺id
		String storeID = paramMap.get("storeId").toString();
		if (!ValidatorUtils.isNumber(storeID)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店铺id");
		}
		// 玩家id
		String userID = null;
		if(null != paramMap.get("userId")){
			userID = paramMap.get("userId").toString();
		}
		// 店员id
		String empId = null;
		if(null != paramMap.get("empId")){
			empId = paramMap.get("empId").toString();
		}
		if (!ValidatorUtils.isNumber(userID) && !ValidatorUtils.isNumber(empId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "玩家id");
		}
		// 游戏id
		String gameID = paramMap.get("gameId").toString();
		if (!ValidatorUtils.isNumber(gameID)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "游戏id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("storeID", storeID);
		map.put("macAddress", paramMap.get("macAddress"));
		map.put("userId", userID);
		map.put("empId", empId);
		map.put("gameID", gameID);
		purchaseHistoryService.addPurchaseHistory(map);
		Map<String,Object> result = Maps.newHashMap();
		result.put("purchaseId", map.get("purchaseID"));
		result.put("detailId", map.get("detailID"));
		return MsgResult.okResultData(result);
	}

	/**
	 * 为消费单添加游戏
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addGame", method = RequestMethod.POST)
	public Map<String, Object> addGame(HttpServletRequest request) {
		String params = request.getParameter("params");
		if(StringUtils.isBlank(params)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "参数");
		}
		Map<String,Object> paramMap = ConvertUtils.jsonToMap(params);
		// 消费id
		if(null == paramMap.get("purchaseId") || "".equals(paramMap.get("purchaseId"))){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "消费订单id");
		}
		String purchaseID = paramMap.get("purchaseId").toString();
		if (!ValidatorUtils.isNumber(purchaseID)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "消费订单id");
		}
		// 游戏id
		if(null == paramMap.get("gameId") || "".equals(paramMap.get("gameId"))){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "游戏id");
		}
		String gameID = paramMap.get("gameId").toString();
		if (!ValidatorUtils.isNumber(gameID)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "游戏id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("gameID", gameID);
		map.put("purchaseID", purchaseID);
		purchaseHistoryService.addPurGame(map);
		map.put("detailId", map.get("detailID"));
		map.remove(gameID);
		return MsgResult.okResultData(map);
	}

	/**
	 * 游戏结束
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "finishGame", method = RequestMethod.POST)
	public Map<String, Object> finishGame(HttpServletRequest request) {
		String params = request.getParameter("params");
		if(StringUtils.isBlank(params)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "参数");
		}
		Map<String,Object> paramMap = ConvertUtils.jsonToMap(params);
		if(null == paramMap.get("type") || "".equals(paramMap.get("type"))){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "结束类型");
		}
		// 消费id
		if(null == paramMap.get("purchaseId") || "".equals(paramMap.get("purchaseId"))){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "消费订单id");
		}
		String purchaseID = paramMap.get("purchaseId").toString();
		if (!ValidatorUtils.isNumber(purchaseID)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "消费订单id");
		}
		// 消费明细id
		if(null == paramMap.get("detailId") || "".equals(paramMap.get("detailId"))){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "消费订单id");
		}
		String detailID = paramMap.get("detailId").toString();
		if (!ValidatorUtils.isNumber(detailID)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "消费订单id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("type", paramMap.get("type").toString());
		map.put("purchaseId", purchaseID);
		map.put("detailID", detailID);
		info("+++++++结束游戏请求参数："+JSON.toJSONString(map));
		purchaseHistoryService.updatePurchaseHistory(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 游戏评价
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "evaluate", method = RequestMethod.POST)
	public Map<String, Object> evaluate(HttpServletRequest request){
		String params = request.getParameter("params");
		if(StringUtils.isBlank(params)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "参数");
		}
		Map<String,Object> paramMap = ConvertUtils.jsonToMap(params);
		if(null == paramMap || paramMap.isEmpty()){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "参数");
		}
		paramMap.put("evaTime", DateTime.now().toDate());
		paramMap.put("isEva", 1);
		paramMap.put("purchaseId", paramMap.get("purchaseId"));
		paramMap.put("evaluate", paramMap.get("evaluate"));
		purchaseHistoryService.updatePurchaseHistoryByEva(paramMap);
		return MsgResult.okResult();
		
	}
	
	/**
	 * 评价列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectEvaList", method = RequestMethod.GET)
	public Map<String, Object> commentList(HttpServletRequest request) {
		return MsgResult.okResultData(purchaseHistoryService.selectEvaluateList());
	}

}
